package com.brook;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.springframework.stereotype.Repository;

public class ActivitiBusinessDemo {


    //绑定businesskey
    @Test
    public void addBusinessKey() {
        //1.获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RuntimeService
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //3.启动流程的过程中，添加businessKey
        ProcessInstance myEvection = runtimeService.startProcessInstanceByKey("myEvection", "1001");
        //4.输出
        System.out.println("businessKey=="+myEvection.getBusinessKey());

    }

    //全部流程挂起
    @Test
    public void suspendAllProcessInstance(){
        //1.获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取Respositoryservice\
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //3.查询流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("myEvection")
                .singleResult();
        //4.获取当前流程定义的实例是否都是挂起状态
        boolean suspended = processDefinition.isSuspended();
        //5.获取流程定义的id
        String id = processDefinition.getId();
        //6.如果时挂起状态，改为激活状态
        if(suspended){
            //如果是挂起，可以执行激活操作,参数1是流程定义id，参数2：是否激活，参数3：激活时间
            repositoryService.activateProcessDefinitionById(id,true, null);
            System.out.println("流程定义id："+id+"，已激活");
        }else{
            //7.如果是激活状态，改为挂起状态,参数1：流程定义id，参数2：是否暂停，参数3：暂停时间
            repositoryService.suspendProcessDefinitionById(id,true,null);
            System.out.println("流程定义id："+id+"，已挂起");
        }
        //7.如果是激活状态，改为挂起状态
    }

    //单个流程实例的挂起
    @Test
    public void suspendSingleProcessInstance(){
        //1.获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RuntimeService
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //3.通过runtimeService获取流程实例对象runtimeService.createProcessInstanceQuery()
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("2501")
                .singleResult();
        //4.获取当前流程实例的暂停状态true是已暂停，false是激活
        boolean suspended = processInstance.isSuspended();
        //5.获取流程实例id
        String id = processInstance.getId();
        //6.判断是否暂停，如果已经暂停，就执行激活操作
        if(suspended){
            runtimeService.activateProcessInstanceById(id);
            System.out.println("流程定义id："+id+"，已激活");
        }else{
            //7.如果是激活状态，就执行暂停操作
            runtimeService.suspendProcessInstanceById(id);
            System.out.println("流程定义id："+id+"，已暂停");
        }
    }

    //挂起状态下执行完成任务操作
    @Test
    public void completeTask(){
        //1.获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取taskService
        TaskService taskService = defaultProcessEngine.getTaskService();
        //3.使用taskService获取任务
        Task task = taskService.createTaskQuery()
                .processInstanceId("27501")
                .taskAssignee("zhangsan")
                .singleResult();
        System.out.println("流程实例id=="+task.getProcessInstanceId());
        System.out.println("流程任务id=="+task.getId());
        System.out.println("负责人=="+task.getAssignee());
        System.out.println("任务名称=="+task.getName());
        //4.根据任务的id完成任务
        taskService.complete(task.getId());
    }



}
